<template>
  <div>

    <h3>文章编辑列表</h3>
    <el-button type="primary" @click="toNew">创建文章</el-button>

    <el-table
        :data="articleList"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="文章ID"
          width="70">
      </el-table-column>
      <el-table-column
          prop="title"
          label="标题"
          width="180">
      </el-table-column>
      <el-table-column label="封面图片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.image" style="width: 60px;height: 60px;">
        </template>
      </el-table-column>
      <el-table-column
          prop="introduce"
          label="介绍">
      </el-table-column>
      <el-table-column
          prop="views"
          label="访问量"
          width="70">
      </el-table-column>
      <el-table-column
          prop="insertTime"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间">
      </el-table-column>


      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="toAlter(scope.row.id)">修改
          </el-button>
          <el-button type="danger" size="mini" @click="toDel(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6 , 12, 24]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>

  </div>
</template>

<script>
import Url from "@/plugins/url";

export default {
  name: "PetArticleList2",
  data() {
    return {
      params: {
        pageNum: 1,
        pageSize: 3,
        title: '',
      },
      total: 0,
      pages: 0,
      prePage: 0,
      nextPage: 0,

      articleList: [],


    }
  },
  methods: {
    // getPetImage(img) {
    //   let url = Url.imgUrl + img;
    //   return url;
    // },
    initArticle() {
      let url = '/petArticle/list';
      this.$axios({
        url: url,
        method: 'get',
        params: this.params,
      }).then(res => {
        if (res.data.code === 200) {
          this.articleList = res.data.data.list;
          this.total = res.data.data.total;
          this.nextPage = res.data.data.nextPage;
          this.prePage = res.data.data.prePage;
          this.pages = res.data.data.pages;
        } else {
          this.$message({
            type: 'success',
            message: res.data.msg,
            duration: 1500,
          });
        }
      })
    },
    handleSizeChange(size) {
      this.params.pageSize = size;
      this.params.pageNum = 1;
      this.initArticle();
    },
    handleCurrentChange(num) {
      this.params.pageNum = num;
      this.initArticle();

    },
    toNew() {
      this.$router.push({
        path: '/articleEditor',
        query: {
          id: 0,
        },
      })
    },
    toAlter(id) {
      this.$router.push({
        path: '/articleEditor',
        query: {
          id: id,
        },
      })
    },
    toDel(id) {
      this.$confirm("即将删除本文章", '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(_ => {
        let url = '/petArticle/' + id;
        this.$axios.delete(url).then(res => {
          if (res.data.code === 200) {
            this.$message({
                  type: 'info',
                  message: res.data.msg,
                }
            );
            this.initArticle();
          } else {
            this.$message({
                  type: 'warning',
                  message: res.data.msg,
                }
            )
          }
        });
      }).catch(_ => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    toBack() {
      this.$router.push({
        path: '/petArticleList',
      })
    },

  },

  created() {
    this.initArticle();
  }
}
</script>

<style scoped>

</style>